# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql
from twisted.enterprise import adbapi

class QascrapPipeline:

    def __init__(self, mysql_config):
        self.dbpool = adbapi.ConnectionPool(
            mysql_config['DRIVER'],
            host=mysql_config['HOST'],
            user=mysql_config['USER'],
            password=mysql_config['PASSWORD'],
            database=mysql_config['DATABASE'],
            # auth_plugin=mysql_config['auth_plugin_map'],
            charset='utf8'
        )

    def process_item(self, item, spider):
        ##这里的方法是用来执行sql语句传输数据
        result = self.dbpool.runInteraction(self.insert_into, item)
        ##这里是用来如果报错，则交给insert_erro处理，解释原因
        result.addErrback(self.insert_erro)
        return item

    @classmethod
    def from_crawler(cls, crawler):
        mysql_config = crawler.settings['MYSQL_DB_CONFIG']
        return cls(mysql_config)

    ##执行sql语句插入方法
    def insert_into(self, cursor, item):
        ##编写mysql插入语句，注意，这里的id为null，代表不填入，倘若你之前创建的时候没勾选自增，那么需要到mysql中去改一下
        sql = ('INSERT INTO pc_info (pc_name,pc_price, url,cpu_xilie, cpu_xinghao, cpu_ruipin, cpu_hexinshu, cpu_huancun, cpu_daihao, cpu_gonghao, neicun_rongliang, neicun_leixing, yingpan_rongliang, yingpan_miaoshu) '
               'VALUES (  %s, %s,%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)')
        args = (
            item['pc_name'],item['pc_price'],item['url'], item['cpu_xilie'], item['cpu_xinghao'], item['cpu_ruipin'], item['cpu_hexinshu'],
            item['cpu_huancun'], item['cpu_daihao'], item['cpu_gonghao'], item['neicun_rongliang'],
            item['neicun_leixing'], item['yingpan_rongliang'], item['yingpan_miaoshu']
        )
        print('sql:', sql)
        cursor.execute(sql, args)



    def insert_erro(self, failure):
        print('========================================')
        print(failure)
        print('============================================================')